﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Incubator
{
    public partial class BuyAndSell : System.Web.UI.UserControl
    {
        List<RadioButton> rb_list = new List<RadioButton>();

        protected void Page_Load(object sender, EventArgs e)
        {

            if (Session["UserID"] == null || (int)Session["UserID"] == -1)
                Response.Redirect("main.aspx");
            //get list of shares from market place

            webRef.User u = (webRef.User)Session["User"];
            webRef.Company c = Global.incuServ.GetCompany(u.company_id);

            webRef.Share[] shares = Global.incuServ.GetMarketplaceShares();
            //show them in the table

            const string group_name = "Market Place";

            rb_list.Clear();

            foreach (webRef.Share s in shares)
            {
                TableCell tc0 = new TableCell();
                TableCell tc1 = new TableCell();
                TableCell tc2 = new TableCell();
                TableCell tc3 = new TableCell();
                TableCell tc4 = new TableCell();
                TableRow tr = new TableRow();

                RadioButton rb = new RadioButton();
                rb.Text = s.company.name;
                rb.GroupName = group_name;
                rb_list.Add(rb);
                tc0.Controls.Add(rb);

                tc1.Controls.Add(new LiteralControl(s.owner.username));
                tc2.Controls.Add(new LiteralControl(s.cost.ToString()));
                tc3.Controls.Add(new LiteralControl(s.quantity.ToString()));
                tc4.Controls.Add(new LiteralControl((s.cost * s.quantity).ToString()));

                tr.Cells.Add(tc0);
                tr.Cells.Add(tc1);
                tr.Cells.Add(tc2);
                tr.Cells.Add(tc3);
                tr.Cells.Add(tc4);
                tblShares.Rows.Add(tr);
            }
        }

        protected void btnBuy_Click(object sender, EventArgs e)
        {
         
        }

        protected void btnSell_Click(object sender, EventArgs e)
        {
            Response.Redirect("Sell.aspx");
        }

        protected void btnGetInfo_Click(object sender, EventArgs e)
        {
            //know which share is selected
            //get list of shares from market place

            webRef.User u = (webRef.User)Session["User"];
            webRef.Company c = Global.incuServ.GetCompany(u.company_id);

            webRef.Share[] shares = Global.incuServ.GetMarketplaceShares();
            //know which share is selected
            int i = 0;
            foreach (RadioButton rb in rb_list)
            {
                if (rb.Checked)
                {
                    //get the share's company
                    Session["CompanyOverview"] = shares[i].company;
                    //jump to the CompanyOverview page
                    Response.Redirect("CompanyOverview.aspx");
                    return;
                }
                i++;
            }
            //no radio button has been selected.
        }

        protected void btnAcquire_Click(object sender, EventArgs e)
        {
            //know which share is selected
            //get list of shares from market place

            webRef.User u = (webRef.User)Session["User"];
            webRef.Company c = Global.incuServ.GetCompany(u.company_id);

            webRef.Share[] shares = Global.incuServ.GetMarketplaceShares();
            //know which share is selected
            int i = 0;
            foreach (RadioButton rb in rb_list)
            {
                if (rb.Checked)
                {
                    try
                    {
                        Global.incuServ.BuyShares(u, shares[i].owner, shares[i].company, shares[i].quantity, shares[i].cost);
                        Response.Redirect("Buy.aspx");
                    }
                    catch (Exception ex)
                    {
                        lbError.Text = "Error: Shares are not transferred successfully!";
                    }

                    return;
                }
                i++;
            }
            //no radio button has been selected.
        }
        
    }
}